package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.smartclass.entity.AdClassAttence;
import cn.sdormitory.smartclass.entity.ClassAttence;
import cn.sdormitory.smartclass.form.StudentClassAttendanceQueryForm;
import cn.sdormitory.smartclass.service.ClassAttenceService;
import cn.sdormitory.smartclass.vo.ClassAttenceVo;
import cn.sdormitory.smartclass.vo.StudentClassAttendanceVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2021/12/20 15:33
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/ClassAttence")
@Api("APP端班级考勤管理接口")
public class AppClassAttenceController {
    @Autowired
    private ClassAttenceService classAttenceService;
    @ApiOperation("getAppClaStuAttenceListByCId => 查询某班级下的所有学员当日某时间段的考勤情况")
    @GetMapping(value = "/getAppClaStuAttenceListByCId")
    public ResultMsg listDorAttence(Long classId, String accessDate,String timeSlot){
        int count=classAttenceService.getAppClaStuAttCount(classId,accessDate,timeSlot);
        List<ClassAttenceVo> classAttenceVoList=new ArrayList<>();

        if(count>0){
            //登记过考勤
            classAttenceVoList=classAttenceService.getAppClaStuAttByClaIdAndSlot(classId,accessDate,timeSlot);
        }else{
            //没有登记考勤
            if("3".equals(timeSlot)){//晚自习只查住宿生
                classAttenceVoList=classAttenceService.getAppClaStayStuByClaId(classId);
            }else{
                classAttenceVoList=classAttenceService.getAppClaStuByClaId(classId);
            }


        }
        return ResultMsg.BY_SUCCESS("获取成功",classAttenceVoList);
    }
    @ApiOperation("create => APP端新增班级考勤信息")
    @PostMapping(value = "/createClassAttence")
    public CommonResult<Integer> create(@RequestBody AdClassAttence adClassAttence) throws IOException {
        int count = classAttenceService.addClassAttence(adClassAttence);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("create => APP端获取班级考勤信息")
    @PostMapping(value = "/getClassAttenceStuByCondition")
    public CommonResult getClassAttenceStuByCondition(@RequestBody ClassAttence condition){
        Map<String, List<String>> result = classAttenceService.getClassAttenceStuByCondition(condition);
        return CommonResult.success(result);
    }

    @ApiOperation("create => APP端获取学生班级考勤信息")
    @PostMapping(value = "/getStudentClassAttendances")
    public CommonResult<CommonPage<StudentClassAttendanceVo>> getStudentClassAttendances(@RequestBody StudentClassAttendanceQueryForm form){
        IPage<StudentClassAttendanceVo> page = classAttenceService.getStudentClassAttendancePage(form);
        return CommonResult.success(CommonPage.restPage(page));
    }
}
